import 'package:flutter/material.dart';
import 'package:whale/helper/utils/util.dart';
import 'package:whale/pages/im/component/im_input_add/index.dart';
import 'package:whale/pages/im/component/im_input_expression/index.dart';
import 'package:whale/pages/im/component/im_input_voice/index.dart';
import 'package:whale/pages/im/util/image.dart';

import 'index.dart';

class ImInputPage extends StatelessWidget {
  ImInputPage({super.key});
  final controller = Get.put(ImInputController());
  final state = Get.find<ImInputController>().state;
  Widget _itemButton(String id, {required String img, VoidCallback? onTap}) {
    return GetBuilder<ImInputController>(
      id: id,
      builder: (controller) {
        return InkWell(
          onTap: onTap,
          child: Image.asset(
            img,
            fit: BoxFit.cover,
            width: 40.r,
            height: 40.r,
          ),
        );
      },
    );
  }

  // 主视图
  Widget _buildView() {
    return Row(
      mainAxisSize: MainAxisSize.min,
      children: [
        _itemButton(
          'voice',
          img: state.style != ImInputStyle.voice
              ? 'chat_voice.png'.imImgPath
              : 'chat_keyboard_text_icon.png'.imImgPath,
          onTap: controller.actionVoice,
        ),
        Expanded(
          child: Padding(
            padding: const EdgeInsets.symmetric(horizontal: 7),
            child: ClipRRect(
              borderRadius: BorderRadius.circular(5),
              child: state.style == ImInputStyle.voice
                  ? ImInputVoicePage()
                  : TextField(
                      controller: state.textCtrl,
                      maxLines: 3,
                      minLines: 1,
                      focusNode: state.focusNode,
                      keyboardType: state.style == ImInputStyle.text
                          ? TextInputType.text
                          : TextInputType.none,
                      onTap: controller.actionInput,
                      autofocus: state.style == ImInputStyle.text &&
                          state.status == ImInputStatus.inputing,
                      decoration: const InputDecoration(
                        fillColor: Color(0xfff8f9fc),
                        filled: true,
                        border: InputBorder.none,
                      ),
                    ),
            ),
          ),
        ),
        _itemButton(
          'emoji',
          img: state.style != ImInputStyle.emoji
              ? 'chat_emoji.png'.imImgPath
              : 'chat_keyboard_text_icon.png'.imImgPath,
          onTap: controller.actionEmoji,
        ),
        GetBuilder<ImInputController>(
            id: 'send',
            builder: (context) {
              return Visibility(
                visible: state.status != ImInputStatus.inputing &&
                    state.status != ImInputStatus.textInputOther,
                child: _itemButton(
                  'add-c',
                  img: state.style != ImInputStyle.add
                      ? 'chat_add.png'.imImgPath
                      : 'chat_keyboard_text_icon.png'.imImgPath,
                  onTap: controller.actionAdd,
                ),
              );
            }),
        GetBuilder<ImInputController>(
            id: 'send',
            builder: (context) {
              return Visibility(
                visible: state.status == ImInputStatus.inputing ||
                    state.status == ImInputStatus.textInputOther,
                child: InkWell(
                  onTap: controller.actionSend,
                  child: Container(
                    width: 45.r,
                    height: 30.r,
                    alignment: Alignment.center,
                    margin: const EdgeInsets.only(left: 3),
                    decoration: BoxDecoration(
                      color: Colors.blue,
                      borderRadius: BorderRadius.circular(3),
                    ),
                    child: Text(
                      '发送',
                      style: TextStyle(
                        color: Colors.white,
                        fontSize: 13.sp,
                      ),
                    ),
                  ),
                ),
              );
            }),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<ImInputController>(
      builder: (_) {
        return Container(
          color: Colors.white,
          padding: const EdgeInsets.symmetric(horizontal: 5, vertical: 6),
          child: SafeArea(
            child: SizedBox(
              width: 1.sw,
              child: _buildView(),
            ),
          ),
        );
      },
    );
  }
}

class IMInputKeyboardView extends StatelessWidget {
  const IMInputKeyboardView({super.key});

  @override
  Widget build(BuildContext context) {
    return GetBuilder<ImInputController>(
      builder: (controller) {
        if (controller.state.status == ImInputStatus.textNoInput) {
          return Container(height: 0);
        }
        switch (controller.state.style) {
          case ImInputStyle.text:
            return Container(height: 0);
          case ImInputStyle.voice:
            return Container();
          case ImInputStyle.emoji:
            return ImInputExpressionPage();
          case ImInputStyle.add:
            return ImInputAddPage();
        }
      },
    );
  }
}
